<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>各行换色</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style-type: none;
            text-align: center;
            padding: 10px 0;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <ul class="list">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>10</li>
    </ul>
    <script>
        // 1.找li节点
        let lisEle = document.querySelectorAll(".list li");
        //    [...lisEle] => 将伪数组转化为真数组，便于使用真数组的forEach
        [...lisEle].forEach((liEle, index) => {
            // 鼠标悬浮
            liEle.addEventListener("mouseover", function () {
                // 只有普通函数this才是指向当前元素
                this.style.backgroundColor = "#999";
            });

            // 鼠标离开
            liEle.addEventListener("mouseout", function () {
                this.style.backgroundColor = ""; // 将style行内样式设置为空字符，代表删除此样式
            });
        });   
    </script>
</body>

</html>